var Jpush = {
	debug: JPUSH_DEBUG,
	isAndroid: (/android/gi).test(window.navigator.appVersion),
	isIOS: (/iphone|ipad/gi).test(window.navigator.appVersion),
	type: function(obj) {
		return Object.prototype.toString.call(arguments[0]).replace('[object ', '').replace(']', '').toLowerCase();
	},
	// this was only support android platform
	stop: function() {
		if (uexWidgetOne.platformName == 'Simulator') return false;
		var params = {

		};
		uexJPush.stopPush(JSON.stringify(params));
	},
	// this was only support android platform
	resume: function() {
		if (uexWidgetOne.platformName == 'Simulator') return false;
		var params = {

		};
		uexJPush.resumePush(JSON.stringify(params));
	},
	setAliasAndTags: function(tags, alias) {
		// if(this.type(tags) != 'array') throw new Error('tags must be an array');
		// if(this.type(alias) != 'string') throw new Error('alias must be a string');
		uexJPush.setAliasAndTags(JSON.stringify({
			alias: alias,
			tags: tags
		}), function(err, data) {
			if (!error) {
				alert(JSON.stringify(data));
			} else {
				alert('设置失败');
			}
		});
	},
	setAlias: function(alias, aliasCallback) {
		// if(this.type(alias) != 'string') throw new Error('alias must be a string');
		uexJPush.setAlias(JSON.stringify({
			alias: alias
		}), function(error, data) {
			if (!error) {
				aliasCallback && aliasCallback.call(Jpush, data);
			} else {
				alert("设置失败");
			}
		});
	},
	setTags: function(tags) {
		// if(this.type(tags) != 'array') throw new Error('tags must be an array');
		uexJPush.setTags(JSON.stringify({
			tags: tags
		}), function(error, data) {
			if (!error) {

			} else {
				alert("设置失败");
			}
		});
	},
	// 参数无
	getRegistrationID: function() {
		uexJPush.getRegistrationID(JSON.stringify({

		}));
	},
	// not found
	reportNotificationOpened: function(msgId) {
		uexJPush.reportNotificationOpened(JSON.stringify({
			msgId: msgId
		}));
	},
	// 参数无
	clearAllNotifications: function() {
		uexJPush.clearAllNotifications(JSON.stringify({

		}));
	},
	clearNotificationById: function(notificationId) {
		uexJPush.clearNotificationById(JSON.stringify({
			notificationId: notificationId
		}));
	},
	setPushTime: function() {
		uexJPush.setPushTime(JSON.stringify({
			weekDays: ['0', '1', '2', '3', '4'],
			startHour: 0,
			endHour: 18
		}));
	},
	setSilenceTime: function() {
		uexJPush.setSilenceTimesetLatestNotificationNumber(JSON.stringify({
			startHour: 0,
			startMinute: 1,
			endHour: 13,
			endMinute: 0
		}));
	},
	setLatestNotificationNumber: function() {
		uexJPush.setLatestNotificationNumber(JSON.stringify({
			maxNum: 4
		}));
	},
	addLocalNotification: function() {
		uexJPush.addLocalNotification(JSON.stringify({
			builderId: 0,
			title: "这是title",
			content: "这是内容",
			extras: {
				"key": "value"
			},
			notificationId: 3,
			broadCastTime: 10000
		}));
	},
	clearLocalNotifications: function() {
		uexJPush.clearLocalNotifications(JSON.stringify({

		}));
	},
	onReceiveRegistration: function(msg) {
		Jpush.debug && alert('onReceiveRegistration: ' + JSON.stringify(msg));
	},
	handleMsg: function(msg) {
		var extras;
		msg = eval('(' + msg + ')');
		if (msg.message) {
			extras = msg.message;
		} else if (msg.extras) {
			extras = msg.extras;
		}
		if (Jpush.type(extras) == 'string') {
			extras = JSON.parse(extras);
		}
		return extras;
	},
	allHandle: function(msg, type) {

	},
	showMask: function(msg) {
		// Jpush.allHandle(msg, 'onReceiveMessage');
		var self = Jpush,
			extras = self.handleMsg(msg),
			distance = extras.distance,
			windName = LS.get('windName'),
			farmDetailPage = 'detail',
			techDetailPage = 'detail',
			type = 'onReceiveMessage';
		// var href = window.location.href;
		// var isIndex = href.indexOf('index/index') != -1;
		// windName = isIndex ? 'root' : windName;
		try {
			// if(msg.msgId) self.reportNotificationOpened(msg.msgId);
			// if(msg.notificationId) self.clearNotificationById(msg.notificationId);
			if (extras.posting_rob_id) { // 抢单

				var postingRobId = extras.posting_rob_id;
				if (postingRobId) {
					if (Jpush.isIOS) {
						LS.set('order_detail_opts',{
							postingRobId: postingRobId,
							distance: distance
						})
						evalScript('root', 'IOSShowMaskContent();');
						evalScript(windName, 'IOSShowMaskContent()');
					} else {
						appcan.c.pub('order_msg', {
							load: {
								postingRobId: postingRobId,
								distance: distance
							}
						});
					}

					setTimeout(function () {
						evalScript(windName, 'maskMsg.showIt();');
						maskMsg.showIt && maskMsg.showIt();
					}, 200)
				}

			} else if (extras._posting_sys_id) { // 系统派单

				var _posting_sys_id = extras._posting_sys_id;
				if (_posting_sys_id) {
					if (Jpush.isIOS) {
						LS.set('order_detail_opts',{
							postingRobId: _posting_sys_id,
							isSystem: true
						})
						evalScript('root', 'IOSShowMaskContent();');
						evalScript(windName, 'IOSShowMaskContent()');
					}else{
						appcan.c.pub('order_msg', {
							load: {
								postingRobId: _posting_sys_id,
								isSystem: true
							}
						});
					}

					setTimeout(function () {
						evalScript(windName, 'maskMsg.showIt();')
						maskMsg.showIt && maskMsg.showIt();
					}, 200)
				}

			} else if (extras.posting_invite_id) { // 邀单

				var postingRobId = extras.posting_invite_id;
				if (postingRobId) {
					if (Jpush.isIOS) {
						LS.set('order_detail_opts',{
							postingRobId: postingRobId,
							distance: distance
						})
						evalScript('root', 'IOSShowMaskContent();');
						evalScript(windName, 'IOSShowMaskContent()');
					}else{
						appcan.c.pub('order_msg', {
							load: {
								postingRobId: postingRobId,
								distance: distance
							}
						});
					}
					setTimeout(function () {
						evalScript(windName, 'maskMsg.showIt();')
						maskMsg.showIt && maskMsg.showIt();
					}, 200)
				}

			} else if (extras.posting_cancel_id_farm) { // 取消订单(农户)

				LS.set('pub_id', extras.posting_cancel_id_farm);
				openWinOffAm('../../html/orderPub/detail');
				evalScript(farmDetailPage, '$("#pane0").trigger("reload");');

			} else if (extras.posting_id_farm) { // 农户

				LS.set('pub_id', extras.posting_id_farm);
				openWinOffAm('../../html/orderPub/detail');
				evalScript(farmDetailPage, '$("#pane0").trigger("reload");');

			} else if (extras.posting_cancel_id_tech) { // 取消订单(农技员)

				LS.set('receive_id', extras.posting_cancel_id_tech);
				openWinOffAm('../../html/orderReceive/detail');
				evalScript(techDetailPage, '$("#pane0").trigger("load");');

			} else if (extras.posting_id_tech) { // 农技员

				LS.set('receive_id', extras.posting_id_tech);
				openWinOffAm('../../html/orderReceive/detail');
				evalScript(techDetailPage, '$("#pane0").trigger("load");');

			} else if (extras.admin_author_id) { // 审核通过

				LS.set('audit_user_id', extras.admin_author_id);
				openWinOffAm('../../html/user/real-check');
			} else if (extras.system_notify_id) { // 后台发送系统推送消息

				LS.set('noti_id', extras.system_notify_id);
				openWinOffAm('../../html/message/detail');
			}
		} catch (e) {
			Jpush.debug && alert(type + '(): ' + JSON.stringify(e));
		}
	},
	onReceiveConnectionChange: function(msg) {
		Jpush.debug && alert('onReceiveConnectionChange: ' + JSON.stringify(msg));
	},
	// this was called when the appcan was ready
	initConfig: function() {
		uexWidgetOne.cbError = function(opId, type, error) {
			Jpush.debug && alert(JSON.stringify(error));
		}
		if (Jpush.isIOS) {
			Jpush.IOS.initConfig();
			return;
		}
		if (uexWidgetOne.platformName == 'Simulator') return false;

		var self = Jpush;
			// this was abandoned
			// uexJPush.init();
		uexJPush.getConnectionState(function(error) {
			if (!error) {
				Jpush.debug && alert("已连上");
			} else {
				Jpush.debug && alert("未连接" + JSON.stringify(error));
			}
		});
		uexJPush.onReceiveRegistration = self.onReceiveRegistration;
		uexJPush.onReceiveConnectionChange = self.onReceiveConnectionChange;

		uexJPush.onReceiveMessage = self.showMask;
		uexJPush.onReceiveNotification = self.showMask;
		uexJPush.onReceiveNotificationOpen = self.showMask;

	},
	/**
	 * [init 设置别名，并根据相应的业务去处理]
	 * @return {[type]} [description]
	 */
	init: function(alias) {
		if (uexWidgetOne.platformName == 'Simulator') return false;
		var self = this;
		if (Jpush.isIOS)
			self.IOS.setAlias(alias);
		else
			self.setAlias(alias);
	},

	IOS: {
		setAlias: function(alias) {
			uexJPush.setAlias(JSON.stringify({
				alias: alias
			}));

			uexJPush.cbSetAlias = function(data) {};
		},

		initConfig: function(argument) {
			//停止推送服务
			function stopPush() {
				uexJPush.stopPush();
			}
			//恢复推送服务
			function resumePush() {
				uexJPush.resumePush();
			}
			//取得应用程序对应的 RegistrationID
			function getRegistrationID() {
				uexJPush.getRegistrationID();
			}
			//获取推送连接状态
			function getConnectionState() {
				uexJPush.getConnectionState();
			}

			window.uexOnload = function(type) {
				if (type == 0) {
					//取得应用程序对应的 RegistrationID
					uexJPush.cbGetRegistrationID = function(data) {}
						//获取连接状态回调
					uexJPush.cbGetConnectionState = function(data) {}


					uexJPush.onReceiveMessage = Jpush.showMask;
					uexJPush.onReceiveNotification = Jpush.showMask;
					uexJPush.onReceiveNotificationOpen = Jpush.showMask;
					//连接状态变化
					uexJPush.onReceiveConnectionChange = function(data) {}
					//应用程序注册监听
					uexJPush.onReceiveRegistration = function(data) {}
				}
			}
		}
	},
}